import { listDept, getDept } from '@/api/system/dept'
import { getClinicId, setClinics, setParentId } from '@/utils/clinic'

const clinic = {
  state: {
    clinicName: '',
    clinicList: {},
    clinicId: getClinicId(),
    // 门诊完整信息
    department: {},
  },
  mutations: {
    SET_CLINIC_ID: (state, clinicId) => {
      state.clinicId = clinicId
    },
    SET_CLINIC: (state, clinicName) => {
      state.clinicName = clinicName
    },
    SET_CLINICList: (state, clinicList) => {
      state.clinicList = clinicList
    },
    SET_DEPARTMENT: (state, department) => {
      state.department = department
    },
  },
  actions: {
    //存门诊信息
    getClinic({ commit }, clinic) {
      return new Promise((resolve, reject) => {
        getDept(clinic)
          .then((res) => {
            setClinics(res.data.deptId)
            setParentId(res.data.parentId)
            commit('SET_CLINIC_ID', res.data.deptId)
            commit('SET_CLINIC', res.data.deptName)
            commit('SET_DEPARTMENT', res.data)
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    getListClinic({}, obj) {
      return new Promise((resolve, reject) => {
        listDept(obj.queryParams, obj.headers)
          .then((res) => {
            resolve(res)
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
  },
}
export default clinic
